﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.Data;
namespace HA.ModFlow.Controls.Windows.MapData
{
    /// <summary>
    /// winConceptualFrame.xaml 的交互逻辑
    /// </summary>
    public partial class winConceptualFrame : Window
    {
        public winConceptualFrame(object data,ChooseConfigeType chooseType,object result)
        {
            InitializeComponent();

            _chooseType = chooseType;

            switch (chooseType)
            {
                case ChooseConfigeType.ConceptModelCoverChoose:

                    List<ConceptualModel> arryModel = (List<ConceptualModel>)data;

                    this._arryConcepModel = arryModel;
                    this.DataContext = this._arryConcepModel;

                    _indexInList = (int[])result;//引用

                    this.Title = "图层选择";
                    this.leftTextBlock.Text = "概念模型";
                    this.rightTextBlock .Text = "层";
                    //title = "图层选择";
                    //leftTextBlock="概念模型";
                    //rightTextBlock = "层";

                    this.LstConcetual.DisplayMemberPath = "Name";
                    Binding bin=new Binding ("Key");
                    this.LstConcetual.Tag = bin;
                    this.LstConcetual.ItemsSource = arryModel;

                    this.LstLayer.DisplayMemberPath = "Name";
                    bin = new Binding("Key");
                    this.LstLayer.Tag = bin;
                    this.LstLayer.ItemsSource = this._arryConcepModel[this.LstConcetual.SelectedIndex].Coverages;
                    break;
                case ChooseConfigeType.Scatter2DValueChoose:

                    Scatter2D scatter2d = (Scatter2D)data;

                    _scatter2d = scatter2d;

                    this.DataContext = _scatter2d;

                    _indexInList = (int[])result;//引用

                    this.Title = "二维离散点值选择";
                    this.leftTextBlock.Text = "离散点数据集";
                    this.rightTextBlock.Text = "值类型";
                    this.LstConcetual.Items.Clear();
                    this.LstConcetual.Items.Add(_scatter2d.Key);

                    //this.LstConcetual.DisplayMemberPath = "Key";
                    //this.LstConcetual.DataContext = _scatter2d;
                    this.LstLayer.Items.Clear();
                    for (int i = 0; i < this._scatter2d.ValueNames.Count; i++)
                    {
                        this.LstLayer.Items.Add(this._scatter2d.ValueNames[i].ToString ());
                    }


                    //this.LstLayer.DataContext = _scatter2d.ValueNames;

                    // title = "二维离散点值选择";
                    //leftTextBlock="离散点数据集";
                    //rightTextBlock = "值类型";

                    break;
                default:

                    this.DialogResult = false;

                    return;
 
            }

           
            //List<ConceptualModel> arryModel = project.MapData.ConceptualModelCL;         
            //this._arryConcepModel = arryModel;
            //this.DataContext = this._arryConcepModel;

        }

      
        ChooseConfigeType _chooseType;

        private List<ConceptualModel> _arryConcepModel = new List<ConceptualModel>();

        private Scatter2D _scatter2d;

        //如果是Scatter2DValueChoose则数组长度为 1；如果是ConceptModelCoverChoose则数组长度为 2 ，第一项表哪个概念模型，第二项表示哪个图层。
        int[] _indexInList;


        //string title = "";

        //string leftTextBlock;

        //string rightTextBlock;



        //private string _keyConceptual = "";

        //public string KeyConceptual
        //{
        //    get { return _keyConceptual; }
        //    set { _keyConceptual = value; }
        //}


        //private string _keyLayer = "";


        //public string KeyLayer
        //{
        //    get { return _keyLayer; }
        //    set { _keyLayer = value; }
        //}

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            //if (this.LstLayer.SelectedIndex == -1)
            //{
            //    this.Close();
            //}

            if (this.LstConcetual.SelectedIndex != -1)
            {

                switch (_chooseType)
                {
                    case ChooseConfigeType.ConceptModelCoverChoose:

                        //ConceptualModel con = new ConceptualModel();
                        //con = (ConceptualModel)this.LstConcetual.SelectedItem;
                        //this.KeyConceptual = con.Key.ToString();

                        if (_indexInList == null)
                        {
                            _indexInList = new int[2];
                            _indexInList[0] = this.LstConcetual.SelectedIndex;
                        }
                        else
                        {
                            _indexInList[0] = this.LstConcetual.SelectedIndex;
                        }


                        break;
                    case ChooseConfigeType.Scatter2DValueChoose:



                        break;
                    default:
                        break;
                }

            }
            if (this.LstLayer.SelectedIndex != -1)
            {

                switch (_chooseType)
                {
                    case ChooseConfigeType.ConceptModelCoverChoose:

                        //Coverage cov = new Coverage();
                        //cov = (Coverage)this.LstLayer.SelectedItem;
                        //this.KeyLayer = cov.Key.ToString();

                        if (_indexInList == null)
                        {
                            _indexInList = new int[2];
                            _indexInList[1] = this.LstLayer.SelectedIndex;
                        }
                        else
                        {
                            _indexInList[1] = this.LstLayer.SelectedIndex;
                        }

                        break;
                    case ChooseConfigeType.Scatter2DValueChoose:


                        if (_indexInList == null)
                        {
                            _indexInList = new int[1];
                            _indexInList[0] = this.LstLayer.SelectedIndex;
                        }
                        else 
                        {
                            _indexInList[0] = this.LstLayer.SelectedIndex;

                        }

                        break;
                    default:
                        break;
                }

            }
            if (this.LstConcetual.SelectedIndex == -1 || this.LstLayer.SelectedIndex == -1)
            {
                this.DialogResult = false;
            }
            else
            {

                this.DialogResult = true;
            }
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.LstLayer.Items.Count > 0)
            {
                this.btnOk.IsEnabled = true;
                this.LstLayer.SelectedIndex = 0;
            }
            else
            {
                this.btnOk.IsEnabled = false;
            }
            if (this.LstConcetual.Items.Count > 0)
            {
                this.LstConcetual.SelectedIndex = 0;
            }
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false ;

            this.Close();
        }

        private void LstConcetual_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_chooseType == ChooseConfigeType.ConceptModelCoverChoose)
            {
                if (this._arryConcepModel.Count > 0)
                {
                    if (this.LstConcetual.SelectedIndex != -1)
                    {
                        this.LstLayer.DisplayMemberPath = "Name";
                        Binding bin = new Binding("Key");
                        this.LstLayer.Tag = bin;
                        this.LstLayer.ItemsSource = this._arryConcepModel[this.LstConcetual.SelectedIndex].Coverages;
                    }
                }
            }
            if (this.LstLayer.Items.Count > 0)
            {
                this.btnOk.IsEnabled = true;             
            }
            else
            {
                this.btnOk.IsEnabled = false;
            }
        }

       
     

    }
}
